// Copyright 2006 Mloofle Inc. All Rights Reserved.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Represents a single file indexed by the corpus; the actual referenced file
 * may or may not exist.
 */
public class CorpusFile {

  public CorpusFile(String filePath, int bytes, int words, int lines) {
    this.filePath = filePath;
    this.bytes = bytes;
    this.words = words;
    this.lines = lines;
  }

  public InputStream createInputStream() {
    try {
      return FileStreamCreator.createInputStream(filePath);
    } catch (FileNotFoundException e) {
      Util.errExit("Corpus file '" + filePath + "' does not exist");
    } catch (IOException e) {
      Util.errExit("Corpus file '" + filePath + "' could not be read from");
    }
    return null;
  }

  public int getBytes() {
    return bytes;
  }

  public String getFilePath() {
    return filePath;
  }

  public int getLines() {
    return lines;
  }

  public int getWords() {
    return words;
  }

  public void runQuery(Query query, List resultList) {
    for (int i = 0; i < lineArray.length; ++i) {
      if (lineArray[i].matchesQuery(query)) {
        resultList.add(new PossibleMatch(this, i + 1));
      }
    }
  }

  void initLines(BitReader reader) throws IOException {
    if (lineArray != null) {
      throw new IllegalStateException();
    }
    lineArray = new CorpusLine[lines];
    for (int i = 0; i < lines; ++i) {
      lineArray[i] = CorpusLine.create(reader);
    }
  }
  private final int bytes;
  private final String filePath;
  private CorpusLine[] lineArray = null;
  private final int lines;
  private final int words;

}
